﻿using CefSharp;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;
using VSHT.sdk;

namespace VSHT
{
    public partial class authorization : Form
    {
        private string paramter;
        private string username = "";
        private Dictionary<string, string> detail;

        private bool authed = true;
        public authorization(string paramter)
        {
            this.paramter = paramter;
            InitializeComponent();
        }

        private void uiSymbolButton2_Click(object sender, EventArgs e)
        {
            try
            {
                if (Form1.myPhone == null)
                {
                    MessageBox.Show("未初始化，请先执行初始化");
                    return;
                }
                Program.MainForm.Invoke(new Action(() =>
                {
                    Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId);
                    Form1.videoForm.Show();

                    Form1.videoForm.BringToFront();
                    panel_video.Controls.Add(Form1.videoForm);

                }));
                string deviceSerialno = detail["deviceserialno"];
                username = deviceSerialno.Substring(3, deviceSerialno.Length);
                Form1.myPhone.callto(username);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        //输入用户名和密码
        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
            string data = "{" + "username:" + this.uiTextBox_username.Text+"," + "password:" + this.uiTextBox_password.Text + "}";
            string javascriptCode = $"this.setFing({data})";
            Form1.browser.GetMainFrame().ExecuteJavaScriptAsync(javascriptCode);
        }
        private bool isRunning;
        private Thread th;
        public void ManageThread()
        {
            isRunning = true;
            th = new Thread(() =>
            {
                while (isRunning)
                {
                    if(Form1.fingerType == "za")
                    {
                        VertifyThread();
                    }
                }
            });
            th.Start();
        }
        /// <summary>
        /// 指纹验证线程
        /// </summary>
        public unsafe void VertifyThread()
        {
            if (Form1.openret != 0) { return; }
            string data;

            while (true)
            {
                int ret = 1;
                int r = 0;
                IntPtr em = new IntPtr();
                r = ZAZApi.fingerDetect(Form1.dev, Form1.naddr);
                if (r != 0) { continue; }
                int g = ZAZApi.getImage(Form1.dev, Form1.naddr);

                if (g == 0)
                {
                    g = ZAZApi.genChar(Form1.dev, Form1.naddr, 1);
                }
                if (g == 0)
                {
                    g = ZAZApi.search(Form1.dev, Form1.naddr, 1, 500, ret, &em);
                }
                if (g == 0)
                {
                    Console.WriteLine(g + em.ToString());
                    data = "{" + "status:0" + "," + "Id:" + em.ToString() + "," + "feature:" + "'指纹验证成'" + "}";
                    string javascriptCode = $"this.setFing({data})";
                    Form1.browser.GetMainFrame().ExecuteJavaScriptAsync(javascriptCode);
                    isRunning = false; // 设置isRunning为false以终止线程
                    th.Join(); // 等待线程结束
                    break;
                }
                if (g == 17)
                {
                    Console.WriteLine("指纹没有匹配");
                    data = "{" + "status:1" + "," + "Id:'null'" + "," + "feature:" + "'指纹没有匹配'" + "}";
                    string javascriptCode = $"this.setFing({data})";
                    Form1.browser.GetMainFrame().ExecuteJavaScriptAsync(javascriptCode);
                    isRunning = false; // 设置isRunning为false以终止线程
                    th.Join(); // 等待线程结束
                    break;
                }
                var res = ZAZApi.err2str(g);
                Console.WriteLine(res + "!!!!!!!!!!!" + g);
                byte[] ys = new byte[1024];
                var restr = System.Text.Encoding.Default.GetString(ys);
                //var restr = new String(res);
                //Thread.Sleep(5);//如果不延时，将占用CPU过高  
            }
        }
        private void authorization_Load(object sender, EventArgs e)
        {
            Dictionary<string, object> pairs = JsonConvert.DeserializeObject<Dictionary<string, object>>(paramter);
            object oarray = pairs["rows"];
            object[] arr = JsonConvert.DeserializeObject<object[]>(JsonConvert.SerializeObject(oarray));
            Dictionary<string, string> detail = JsonConvert.DeserializeObject<Dictionary<string, string>>(JsonConvert.SerializeObject(arr[0]));
            this.uiLabel_apply_time.Text = detail["approveTime"];
            this.uiLabel_content.Text = detail["content"];
            this.uiLabel_reson.Text = detail["sy"];
            ManageThread();
        }

        private void uiSymbolButton3_Click(object sender, EventArgs e)
        {
            Form1.myPhone.close();
            this.Dispose();
        }

        private void uiRadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            this.authed = true;
        }

        private void uiRadioButton2_CheckedChanged(object sender, EventArgs e)
        {
            this.authed = false;
        }
    }
}
